<?php
/*
 * 方法名：getTree
 * 作  用：无限级分类
 * 参  数：$data = 数据集
 *        $pid = 最顶级id
 *        $count = 统计
 * */
function getTree(&$data,$pid = 0,$count = 0) {
    if(!isset($data['odl'])){
        $data=array('new'=>array(),'odl'=>$data);
    }
    foreach ($data['odl'] as $k => $v){
        if($v['menu_parent']==$pid){
            $v['Count'] = $count;
            $data['new'][]=$v;
            unset($data['odl'][$k]);
            getTree($data,$v['id'],$count+1);
        }
    }
    return $data['new'] ;
}

function phpInfoShow($varName) {
    switch($result = get_cfg_var($varName)) {
        case 0:
            return '<font color="red">×</font>';
            break;
        case 1:
            return '<font color="green">√</font>';
            break;
        default:
            return $result;
            break;
    }
}
function logAdmin($id){
    if(empty($id)){
        return false;
    }else{
        return true;
    }
}

function encode_json($str){
    return preg_replace("#\\\u([0-9a-f]{4}+)#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $str);
}

function isLogin(){
    $uid=session('adminId');
    return logAdmin($uid) ? $uid : false;
}


/**
 * 把返回的数据集转换成Tree
 * @param array $list 要转换的数据集
 * @param string $pid parent标记字段
 * @param string $level level标记字段
 * @return array
 */
function list_to_tree($list,$pk='id',$pid='pid',$child='child',$root=0){
    $tree=array();// 创建Tree
    if(is_array($list)){
        $refer=array();// 创建基于主键的数组引用
        foreach($list as $key=>$data) $refer[$data[$pk]] =& $list[$key];
        foreach($list as $key=>$data){
            $parentId=$data[$pid];// 判断是否存在parent
            if($root==$parentId) $tree[]=&$list[$key];
            else{
                if(isset($refer[$parentId])){
                    $parent=&$refer[$parentId];
                    $parent[$child][]=&$list[$key];
                }
            }
        }
    }
    return $tree;
}